package cn.ac.registAService.commons.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.ac.registAService.exception.BusinessException;
import cn.ac.registAService.model.MetadataIso;
import cn.ac.registAService.model.User;
import cn.ac.registAService.service.impl.MetadataManagerImpl;

//import bsh.This;

/**
 * @author yangzhong E-mail: zyang@ceode.ac.cn
 * @version 1.0
 * @date 2012-10-16 下午4:26:19
 * @fuction 为系统提供关于操作csw方面的支持
 */

public class CswTool {

	static Logger logger=Logger.getLogger(CswTool.class.getName());
	
	// ---------------------------------------------------------------------------
	// ---
	// --- private 属性
	// ---
	//
	private static String FS = File.separator;

	//path of system config file
	private static String strConfig=ConfigReader.getConfPath();
	// cswTemplet路径
	public static String cswTempletePath = ConfigReader.getCswTemplete();
	
	// csw服务器Ip
	private static String strIp = PropertyReader.readValue(
			strConfig, "csw.ip");
	// csw服务器端口
	private static String strPort = PropertyReader.readValue(
			strConfig, "csw.port");
	// csw服务器登录用户名
	private static String strUserName = PropertyReader.readValue(
			strConfig, "csw.userName");
	// csw服务器登录密码
	private static String strPassword = PropertyReader.readValue(
			strConfig, "csw.password");

	// csw url
	// String serviceUrl="http://localhost:8080/geonetwork/srv/en/csw";
	private static String cswUrl = "http://" + strIp + ":" + strPort
			+ "/geonetwork/srv/en/csw";

	// csw登录地址，如http://localhost:8080/geonetwork/srv/en/xml.user.login
	private static String loginUrl = "http://" + strIp + ":" + strPort
			+ "/geonetwork/srv/en/xml.user.login";

	// csw登出地址 String
	// logoutUrl="http://localhost:8080/geonetwork/srv/en/xml.user.logout";
	private static String logoutUrl = "http://" + strIp + ":" + strPort
			+ "/geonetwork/srv/en/xml.user.logout";

	//元数据模版xml
	public static String metadataXmlPath=CswTool.cswTempletePath+FS+"metadataiso19139.xml";
	//查询资源xml（GetRecordById）
	public static String queryByIdFilePath=cswTempletePath + FS+ "csw-GetRecordById.xml";
	//查询资源xml（GetRecordById）
	public static String getRecordsFilePath=cswTempletePath + FS+ "csw-GetRecords.xml";
	//添加资源xml（不含元数据）
	public static String insertXmlPath = cswTempletePath + FS+ "csw-TransactionInsert.xml";
	//删除资源xml
	public static String deleteFilePath = cswTempletePath + FS+ "csw-TransactionDelete.xml";
	//修改资源xml
	public static String updateFilePath= cswTempletePath + FS+ "csw-TransactionUpdate.xml";
	

	// ---------------------------------------------------------------------------
	// ---
	// --- public 属性
	// ---
	//
	
	// 2.1标识信息
//	// 语言xmlPath
//	public static String languageXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:language/gco:CharacterString";
//	// 资源名称
//	public static String titleXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString";
//	// 资源描述 abstract(metadataAbstract)
//	public static String metadataAbstractXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString";
//	// 关键字 keyword(metadataKeyword)
//	public static String metadataKeywordXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString";
//	// 时间范围-开始时间
//	public static String beginPositionXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition";
//	// 时间范围-结束时间
//	public static String endPositionXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition";
//	// 空间范围-北纬 参考 northBoundLatitude
//	public static String northBoundLatitudeXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:northBoundLatitude/gco:Decimal";
//	// 空间范围-南纬 southBoundLatitude
//	public static String southBoundLatitudeXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:southBoundLatitude/gco:Decimal";
//	// 空间范围-西经 westBoundLongitude
//	public static String westBoundLongitudeXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:westBoundLongitude/gco:Decimal";
//	// 空间范围-东经 eastBoundLongitude
//	public static String eastBoundLongitudeXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:eastBoundLongitude/gco:Decimal";
//	// 节点类型 topicCategory
//	public static String topicCategoryXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:topicCategory/gmd:MD_TopicCategoryCode";
//	// 发布时间date(publicationDate)
//	public static String publicationDateXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:date/gmd:CI_Date/gmd:date/gco:DateTime";

	
	// 语言xmlPath
	public static String languageXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:language/gco:CharacterString";
	// 资源名称
	public static String titleXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString";
	// 资源描述 abstract(metadataAbstract)
	public static String metadataAbstractXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString";
	// 关键字 keyword(metadataKeyword)
	public static String metadataKeywordXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString";
	// 时间范围-开始时间
	public static String beginPositionXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:beginPosition";
	// 时间范围-结束时间
	public static String endPositionXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod/gml:endPosition";
	// 空间范围-北纬 参考 northBoundLatitude
	public static String northBoundLatitudeXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:northBoundLatitude/gco:Decimal";
	// 空间范围-南纬 southBoundLatitude
	public static String southBoundLatitudeXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:southBoundLatitude/gco:Decimal";
	// 空间范围-西经 westBoundLongitude
	public static String westBoundLongitudeXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:westBoundLongitude/gco:Decimal";
	// 空间范围-东经 eastBoundLongitude
	public static String eastBoundLongitudeXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:eastBoundLongitude/gco:Decimal";
	// 节点类型 topicCategory
	public static String topicCategoryXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:topicCategory/gmd:MD_TopicCategoryCode";
	// 发布时间date(publicationDate)
	public static String publicationDateXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:date/gmd:CI_Date/gmd:date/gco:DateTime";
	//资源使用限制-其他限制节点下包括的信息（数据类型、数据量大小、数据条目个数、）
	private static String otherConstraintsXmlPath="./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:resourceConstraints/gmd:MD_LegalConstraints/gmd:otherConstraints";
	//补充信息
	private static String supplementXmlPath="./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:supplementalInformation/gco:CharacterString";
	
//	// 2.2发布信息
//	// 访问地址/服务访问点linkage
//	public static String linkageXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource/gmd:linkage/gmd:URL";
//	// 服务类型/服务模型 protocol
//	public static String protocolXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource/gmd:protocol/gco:CharacterString";

	// 2.2发布信息
	// 访问地址/服务访问点linkage
	//public static String linkageXmlPath = "./gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource/gmd:linkage/gmd:URL";
	public static String linkageXmlPath = "./gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine";
	// 服务类型/服务模型 protocol
	//public static String protocolXmlPath = "./gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource/gmd:protocol/gco:CharacterString";
	public static String protocolXmlPath = "./gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine";
	
//	// 2.3数据质量信息
//	// 质量描述quality
//	public static String qualityXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:dataQualityInfo/gmd:DQ_DataQuality/gmd:lineage/gmd:LI_Lineage/gmd:statement/gco:CharacterString";

	// 2.3数据质量信息
	// 质量描述quality
	public static String qualityXmlPath = "./gmd:dataQualityInfo/gmd:DQ_DataQuality/gmd:lineage/gmd:LI_Lineage/gmd:statement/gco:CharacterString";

	
//	// 2.4元数据信息
//	// 标识符UDDI fileIdentifier
//	public static String fileIdentifierXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:fileIdentifier/gco:CharacterString";
//	// 修改时间dateStamp(modifyDate)
//	public static String modifyDateXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:dateStamp/gco:DateTime";

	// 2.4元数据信息
	// 标识符UUID fileIdentifier
	public static String fileIdentifierXmlPath = "./gmd:fileIdentifier/gco:CharacterString";
	// 修改时间dateStamp(modifyDate)
	public static String modifyDateXmlPath = "./gmd:dateStamp/gco:DateTime";

	
//	// 2.5 资源组织
//	// 组织名称organisationName
//	public static String organisationNameXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString";
//	// 负责人姓名individualName
//	public static String individualNameXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString";
//	// 联系电话 voice
//	public static String voiceXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:phone/gmd:CI_Telephone/gmd:voice/gco:CharacterString";
//	// 详细地址 deliveryPoint
//	public static String deliveryPointXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:deliveryPoint/gco:CharacterString";
//	// 城市（兰州市） city
//	public static String cityXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:city/gco:CharacterString";
//	// 行政区域(甘肃省)administrativeArea
//	public static String administrativeAreaXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:administrativeArea/gco:CharacterString";
//	// 邮政编码postalCode
//	public static String postalCodeXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:postalCode/gco:CharacterString";
//	// 国家/组织所在地 country
//	public static String countryXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:country/gco:CharacterString";
//	// 电子邮件electronicMailAddress
//	public static String electronicMailAddressXmlPath = "/csw:Transaction/csw:Insert/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString";

	// 2.5 资源组织
	// 组织名称organisationName
	public static String organisationNameXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString";
	// 负责人姓名individualName
	public static String individualNameXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString";
	// 联系电话 voice
	public static String voiceXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:phone/gmd:CI_Telephone/gmd:voice/gco:CharacterString";
	// 详细地址 deliveryPoint
	public static String deliveryPointXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:deliveryPoint/gco:CharacterString";
	// 城市（兰州市） city
	public static String cityXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:city/gco:CharacterString";
	// 行政区域(甘肃省)administrativeArea
	public static String administrativeAreaXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:administrativeArea/gco:CharacterString";
	// 邮政编码postalCode
	public static String postalCodeXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:postalCode/gco:CharacterString";
	// 国家/组织所在地 country
	public static String countryXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:country/gco:CharacterString";
	// 电子邮件electronicMailAddress
	public static String electronicMailAddressXmlPath = "./gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString";

	
	
	
	// ---------------------------------------------------------------------------
	// ---
	// --- method
	// ---
	//
	
	/**
	 * csw操作
	 * @param requestXml 请求字符串xml格式
	 * @return 返回请求结果字符串xml格式
	 * @throws BusinessException
	 */
	public static String loginAndRun(String requestXml) throws BusinessException  {
		PostMethod post = null;
		HttpClient httpClient=null;
		try {
			httpClient = new HttpClient();

			// ---1logout
			// first-------------------------------------------------------------------------
			// 1.1create logout request xml
			String requestLogout = "<?xml version='1.0' encoding='UTF-8'?>"
					+ "\n" + "<request/>";
			// 1.2csw login url
			post = new PostMethod(logoutUrl);
			logger.info("登出地址：" + logoutUrl);
			// 1.3发送请求信息
			post.setRequestEntity(new StringRequestEntity(requestLogout,
					"application/xml", "UTF8"));
			int resultLogout = httpClient.executeMethod(post);
			logger.info("登出Response status code: " + resultLogout);
			// 1.4响应应信息（response）
			logger.info("登出response:" + "\n" + getResponseBody(post));

			// ---2login-------------------------------------------------------------------------
			// 2.1create login request xml
			Document requestDocument = DocumentHelper.createDocument();
			Element requestRoot = requestDocument.addElement("request");
			Element username = requestRoot.addElement("username");
			username.setText(strUserName);
			Element password = requestRoot.addElement("password");
			password.setText(strPassword);
			String requestLogin = requestDocument.asXML();
			// 2.2csw login url
			post = new PostMethod(CswTool.loginUrl);
			logger.info("登录地址：" + CswTool.loginUrl);
			// 2.3发送请求信息（set request）
			post.setRequestEntity(new StringRequestEntity(requestLogin,
					"application/xml", "UTF8"));
			int resultLogin = httpClient.executeMethod(post);
			logger.info("登录Response status code: " + resultLogin);
			// 响应应信息（response）
			logger.info("登录response:" + "\n" + getResponseBody(post));
		} 
		catch (IOException e) {
			throw new BusinessException("无法登陆CSW服务器，检查服务器地址是否正确："+CswTool.loginUrl);        
		}
//		finally {
//			// release current connetion
//			if(post!=null)
//			{
//				post.releaseConnection();
//			}			
//		}
        
		try{
			// ---3run--------------------------------------------------------------------------------------
			// 3.1 csw url
			post = new PostMethod(cswUrl);
			logger.info(cswUrl);
			// 3.2发送请求信息（set request）
			post.setRequestEntity(new StringRequestEntity(requestXml,
					"application/xml", "UTF8"));
			int result = httpClient.executeMethod(post);
			logger.info("CSW操作 Response status code: " + result);
			// 3.3响应应信息（response）
			String response = getResponseBody(post);
			return response;
		} 
		catch (IOException e) {
			throw new BusinessException("CSW操作错误：");        
		}
		finally {
			// release current connetion
			if(post!=null)
			{
				post.releaseConnection();
			}			
		}

	}
	

	/**
	 * PostMethod的返回内容（解决以下错误）
	 *[WARN org.apache.commons.httpclient.HttpMethodBase] Going to buffer response body of
	 *large or unknown size. Using getResponseBodyAsStream instead is recommended.
	 * @param post
	 * @return  返回的内容
	 */
	private static String getResponseBody(PostMethod post) 
	{
		try {
			InputStream responseIs = post.getResponseBodyAsStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(responseIs,"UTF-8"));
			StringBuffer responseBuffer = new StringBuffer();
			String responseTemp = "";
			while ((responseTemp = br.readLine()) != null) {
				responseBuffer.append(responseTemp+"\n");
			}
			return responseBuffer.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

    /**
     * 设置资源元数据值
     * @param metadataElement  元数据模版根元素gmd:MD_Metadata
     * @param metadataIso  资源实体
     * @return 返回设置好值的metadataElement
     */
	public static Element setMetadata(Element metadataElement,MetadataIso metadataIso) throws BusinessException
	{			
		// 2.1设置标识信息
		// 语言
		Element languageElement = (Element) metadataElement
				.selectSingleNode(CswTool.languageXmlPath);
		if(metadataIso.getLanguage()==null)    //set default value
			languageElement.setText("简体中文");
		else
		    languageElement.setText(metadataIso.getLanguage());
		logger.info("语言name：" + languageElement.getName() + ",value："
				+ languageElement.getTextTrim());

		// 资源名称title
		Element titleElement = (Element) metadataElement
				.selectSingleNode(CswTool.titleXmlPath);
		if(metadataIso.getTitle()==null)
			titleElement.setText("null");  //set default value
		else
		    titleElement.setText(metadataIso.getTitle());
		logger.info("资源名称name：" + titleElement.getName() + ",value："
				+ titleElement.getTextTrim());

		// 资源描述 abstract(metadataAbstract)
		Element metadataAbstractElement = (Element) metadataElement
				.selectSingleNode(CswTool.metadataAbstractXmlPath);
		if(metadataIso.getMetadataAbstract()==null)
			metadataAbstractElement.setText("null"); //set default value
		else
		    metadataAbstractElement.setText(metadataIso.getMetadataAbstract());
		logger.info("资源描述name：" + metadataAbstractElement.getName()
				+ ",value：" + metadataAbstractElement.getTextTrim());

		// 关键字 keyword(metadataKeyword)
		Element metadataKeywordElement = (Element) metadataElement
				.selectSingleNode(CswTool.metadataKeywordXmlPath);
		if(metadataIso.getMetadataKeyword()==null)
			metadataKeywordElement.setText("null"); //set default value
		else
		    metadataKeywordElement.setText(metadataIso.getMetadataKeyword());
		logger.info("关键字 name：" + metadataKeywordElement.getName()
				+ ",value：" + metadataKeywordElement.getTextTrim());

		// 时间范围-开始时间 beginPosition 2012-10-10T15:39:00
		Element beginPositionElement = (Element) metadataElement
				.selectSingleNode(CswTool.beginPositionXmlPath);
		if(metadataIso.getBeginPosition()==null)
			beginPositionElement.setText("null");
		else
		    beginPositionElement.setText(DateTimeTool.getXMLDateTime(metadataIso.getBeginPosition()));
		logger.info("时间范围-开始时间name：" + beginPositionElement.getName()
				+ ",value：" + beginPositionElement.getTextTrim());

		// 时间范围-结束时间 endPosition 2012-10-10T15:39:00
		Element endPositionElement = (Element) metadataElement
				.selectSingleNode(CswTool.endPositionXmlPath);
		if(metadataIso.getEndPosition()==null)
			endPositionElement.setText("null");
		else
		    endPositionElement.setText(DateTimeTool.getXMLDateTime(metadataIso.getEndPosition()));
		logger.info("时间范围-结束时间 name：" + endPositionElement.getName()
				+ ",value：" + endPositionElement.getTextTrim());

		// 空间范围-北纬 参考 northBoundLatitude GN图上选择空间区域
		Element northBoundLatitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.northBoundLatitudeXmlPath);
		northBoundLatitudeElement.setText(Double.toString(metadataIso
				.getNorthBoundLatitude()));
		logger.info("空间范围-北纬name：" + northBoundLatitudeElement.getName()
				+ ",value：" + northBoundLatitudeElement.getTextTrim());

		// 空间范围-南纬 southBoundLatitude
		Element southBoundLatitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.southBoundLatitudeXmlPath);
		southBoundLatitudeElement.setText(Double.toString(metadataIso
				.getSouthBoundLatitude()));
		logger.info("空间范围-南纬name：" + southBoundLatitudeElement.getName()
				+ ",value：" + southBoundLatitudeElement.getTextTrim());

		// 空间范围-西经 westBoundLongitude
		Element westBoundLongitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.westBoundLongitudeXmlPath);
		westBoundLongitudeElement.setText(Double.toString(metadataIso
				.getWestBoundLongitude()));
		logger.info("空间范围-西经name：" + westBoundLongitudeElement.getName()
				+ ",value：" + westBoundLongitudeElement.getTextTrim());

		// 空间范围-东经 eastBoundLongitude
		Element eastBoundLongitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.eastBoundLongitudeXmlPath);
		eastBoundLongitudeElement.setText(Double.toString(metadataIso
				.getEastBoundLongitude()));
		logger.info("空间范围-东经name：" + eastBoundLongitudeElement.getName()
				+ ",value：" + eastBoundLongitudeElement.getTextTrim());

		// 分类/节点类型/服务类别 topicCategory
		Element topicCategoryElement = (Element) metadataElement
				.selectSingleNode(CswTool.topicCategoryXmlPath);
		if(metadataIso.getTopicCategory()==null)
			topicCategoryElement.setText("其他");
		else
		    topicCategoryElement.setText(metadataIso.getTopicCategory());
		logger.info("节点类型name：" + topicCategoryElement.getName()
				+ ",value：" + topicCategoryElement.getTextTrim());

		// 发布时间date(publicationDate)
		Element publicationDateElement = (Element) metadataElement
				.selectSingleNode(CswTool.publicationDateXmlPath);
		if(metadataIso.getPublicationDate()==null)
			publicationDateElement.setText(null);
		else						
		    publicationDateElement.setText(DateTimeTool.getXMLDateTime(metadataIso
				.getPublicationDate()));
		logger.info("发布时间name：" + publicationDateElement.getName()
				+ ",value：" + publicationDateElement.getTextTrim());

		//获取资源限制信息(数据类型、数据量大小、数据条目个数)
		List<Element> otherConstraintsElList=metadataElement.selectNodes(CswTool.otherConstraintsXmlPath);
		if(otherConstraintsElList!=null && otherConstraintsElList.size()>0)
		{
			//数据类型
			if(metadataIso.getDataType()==null)
			  otherConstraintsElList.get(0).setText("null");	
			else
			  otherConstraintsElList.get(0).setText(metadataIso.getDataType());
			logger.info("数据类型："+otherConstraintsElList.get(0).getTextTrim());
			//数据量大小
			if(metadataIso.getDataSize()==null)
				otherConstraintsElList.get(1).setText("null");
			else
				otherConstraintsElList.get(1).setText(metadataIso.getDataSize());
			logger.info("数据量大小："+otherConstraintsElList.get(1).getTextTrim());
			//数据条目个数
			if(metadataIso.getDataNumber()==null)
				otherConstraintsElList.get(2).setText("null");
			else
				otherConstraintsElList.get(2).setText(metadataIso.getDataNumber());
			logger.info("数据条目个数："+otherConstraintsElList.get(2).getTextTrim());
			//传感器
			if(metadataIso.getSensor()==null)
				otherConstraintsElList.get(3).setText("null");
			else
				otherConstraintsElList.get(3).setText(metadataIso.getSensor());
			logger.info("传感器："+otherConstraintsElList.get(3).getTextTrim());
		} //end if
		
		//补充信息
		Element supplementElement=(Element)metadataElement.selectSingleNode(CswTool.supplementXmlPath);
		if(metadataIso.getSupplement()==null)
			supplementElement.setText("null");
		else
			supplementElement.setText(metadataIso.getSupplement());
		logger.info("补充信息name：" + supplementElement.getName() + ",value："+ supplementElement.getTextTrim());
		
		// 2.2发布信息
		
		//---add---------------
		/**
		 * add the following element
		 					<gmd:onLine>
								<gmd:CI_OnlineResource>
									<!--访问地址-->
									<gmd:linkage>
										<gmd:URL>访问地址/服务访问点</gmd:URL>
									</gmd:linkage>
									<!--服务类型-->
									<gmd:protocol>
										<gco:CharacterString>OGC:WFS-1.0.0-http-get-capabilities</gco:CharacterString>
									</gmd:protocol>
									<gmd:name gco:nilReason="missing">
										<gco:CharacterString/>
									</gmd:name>
								</gmd:CI_OnlineResource>
							</gmd:onLine>
		 */
		Set<String> lingkageInterface=metadataIso.getLinkageInterface();
		if(lingkageInterface!=null && lingkageInterface.size()>0){
			String parentXmlPath="./gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions";
			Element parentEle=(Element)metadataElement.selectSingleNode(parentXmlPath);			
			
			for(int i=0;i<lingkageInterface.size();i++){ //add linkageInterface element
				Element onLineEle=parentEle.addElement("gmd:onLine");
				Element onlineResourceEle=onLineEle.addElement("gmd:CI_OnlineResource"); 
				
				Element linkageEle=onlineResourceEle.addElement("gmd:linkage");
				linkageEle.addElement("gmd:URL");
				
				Element protocolEle=onlineResourceEle.addElement("gmd:protocol");
				protocolEle.addElement("gco:CharacterString");
				
				Element nameEle=onlineResourceEle.addElement("gmd:name");
				nameEle.addAttribute("gco:nilReason", "missing");
				nameEle.addElement("gco:CharacterString");
			} //end for
		} //end if
		
		//---and add---------------
		
		// 访问地址/服务访问点linkage
		List<Element> linkageElList =metadataElement.selectNodes(CswTool.linkageXmlPath);
		if(linkageElList!=null && linkageElList.size()>0)
		{
			String urlXmlPath="./gmd:CI_OnlineResource/gmd:linkage/gmd:URL";               	//服务地址
			String proXmlPath="./gmd:CI_OnlineResource/gmd:protocol/gco:CharacterString";   //服务类型
			
			Element linkageElement=(Element)linkageElList.get(0).selectSingleNode(urlXmlPath);
			Element protocolElement=(Element)linkageElList.get(0).selectSingleNode(proXmlPath);
			//访问地址/服务访问点linkage
			if(metadataIso.getLinkage()==null)
				linkageElement.setText("null");
			else
				linkageElement.setText(metadataIso.getLinkage());
			logger.info("服务地址name：" + linkageElement.getName() + ",value："
			+ linkageElement.getTextTrim());
			// 服务类型/服务模型 protocol
			if(metadataIso.getProtocol()==null)
				protocolElement.setText("其他");
			else
				protocolElement.setText(metadataIso.getProtocol());
			logger.info("服务类型name：" + protocolElement.getName() + ",value："
			+ protocolElement.getTextTrim());
							
			//portal访问地址
			Element portalLinkageElement=(Element)linkageElList.get(1).selectSingleNode(urlXmlPath);
			if(metadataIso.getPortalLinkage()==null)
				portalLinkageElement.setText("null");
			else
				portalLinkageElement.setText(metadataIso.getPortalLinkage());
			logger.info("Portal访问地址name：" + portalLinkageElement.getName() + ",value："
			+ portalLinkageElement.getTextTrim());
			
			//--add------------------
			//服务操作接口
            Object[] lingkageInterfaceArr=lingkageInterface.toArray();
			for(int i=0;i<lingkageInterfaceArr.length;i++){
				Element lingkageInterfaceElement=(Element)linkageElList.get(i+2).selectSingleNode(urlXmlPath);
				if(lingkageInterfaceArr[i]==null)
					portalLinkageElement.setText("null");
				else
					lingkageInterfaceElement.setText((String)lingkageInterfaceArr[i]);
				logger.info("服务端服务接口name：" + lingkageInterfaceElement.getName() + ",value："
						+ lingkageInterfaceElement.getTextTrim());
			} //end for
			//--end add------------------
		} //end if
				
		// 2.3数据质量信息
		// 质量描述
		Element qualityElement = (Element) metadataElement
				.selectSingleNode(CswTool.qualityXmlPath);
		if(metadataIso.getQuality()==null)
			qualityElement.setText("null");
		else
		    qualityElement.setText(metadataIso.getQuality());
		logger.info("质量信息name：" + qualityElement.getName() + ",value："
				+ qualityElement.getTextTrim());

		// 2.4元数据信息
		// 标识符UDDI fileIdentifier 由GN自动生成
		Element fileIdentifierElement = (Element) metadataElement
				.selectSingleNode(CswTool.fileIdentifierXmlPath);
		fileIdentifierElement.setText(UUID.randomUUID().toString());
		logger.info("标识符UDDIname：" + fileIdentifierElement.getName()
				+ "，value：" + fileIdentifierElement.getTextTrim());

		// 字符集 Character（utf8） characterSet 默认utf8，界面显示UTF8

		// 修改时间dateStamp(modifyDate) 格式（YYYY-MM-DDThh:mm:ss）2012-10-11T17:27:20
		Element modifyDateElement = (Element) metadataElement
				.selectSingleNode(CswTool.modifyDateXmlPath);
		if(metadataIso.getModifyDate()==null)
			modifyDateElement.setText("null");
		else
		    modifyDateElement.setText(DateTimeTool.getXMLDateTime(metadataIso.getModifyDate()));
		logger.info("修改时间name：" + modifyDateElement.getName()
				+ ",value：" + modifyDateElement.getTextTrim());

		// 元数据标准名 默认

		// 元数据标准版本 Metadata standard version 默认1.0

		// 2.5 资源组织
		// 组织名称organisationName
		Element organisationNameElement = (Element) metadataElement
				.selectSingleNode(CswTool.organisationNameXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getOrganisationName()==null)
			organisationNameElement.setText("null");
		else
		   organisationNameElement.setText(metadataIso.getOrganizor().getOrganisationName());
		logger.info("组织名称name：" + organisationNameElement.getName()
				+ ",value：" + organisationNameElement.getTextTrim());

		// 负责人姓名individualName
		Element individualNameElement = (Element) metadataElement
				.selectSingleNode(CswTool.individualNameXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getIndividualName()==null)
			individualNameElement.setText("null");
		else
		    individualNameElement.setText(metadataIso.getOrganizor()
				.getIndividualName());
		logger.info("负责人姓名name：" + individualNameElement.getName()
				+ ",value：" + individualNameElement.getTextTrim());

		// 联系电话 voice
		Element voiceElement = (Element) metadataElement
				.selectSingleNode(CswTool.voiceXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getVoice()==null)
			voiceElement.setText("null");
		else
		    voiceElement.setText(metadataIso.getOrganizor().getVoice());
		logger.info("联系电话name：" + voiceElement.getName() + ",value："
				+ voiceElement.getTextTrim());

		// 详细地址 deliveryPoint
		Element deliveryPointElement = (Element) metadataElement
				.selectSingleNode(CswTool.deliveryPointXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getDeliveryPoint()==null)
			deliveryPointElement.setText("null");
		else
		    deliveryPointElement.setText(metadataIso.getOrganizor().getDeliveryPoint());
		logger.info("详细地址name：" + deliveryPointElement.getName()
				+ ",value：" + deliveryPointElement.getTextTrim());

		// 城市（兰州市） city
		Element cityElement = (Element) metadataElement
				.selectSingleNode(CswTool.cityXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getCity()==null)
			cityElement.setText("null");
		else
		    cityElement.setText(metadataIso.getOrganizor().getCity());
		logger.info("城市name：" + cityElement.getName() + ",value："
				+ cityElement.getTextTrim());

		// 行政区域(甘肃省)administrativeArea
		Element administrativeAreaElement = (Element) metadataElement
				.selectSingleNode(CswTool.administrativeAreaXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getAdministrativeArea()==null)
			administrativeAreaElement.setText("null");
		else
		    administrativeAreaElement.setText(metadataIso.getOrganizor()
				.getAdministrativeArea());
		logger.info("行政区域name：" + administrativeAreaElement.getName()
				+ ",value：" + administrativeAreaElement.getTextTrim());

		// 邮政编码postalCode
		Element postalCodeElement = (Element) metadataElement
				.selectSingleNode(CswTool.postalCodeXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getPostalCode()==null)
			postalCodeElement.setText("null");
		else
		    postalCodeElement.setText(metadataIso.getOrganizor().getPostalCode());
		logger.info("邮政编码name：" + postalCodeElement.getName()
				+ ",value：" + postalCodeElement.getTextTrim());

		// 国家/组织所在地 country
		Element countryElement = (Element) metadataElement
				.selectSingleNode(CswTool.countryXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getCountry()==null)
			countryElement.setText("null");
		else
		    countryElement.setText(metadataIso.getOrganizor().getCountry());
		logger.info("国家name：" + countryElement.getName() + ",国家value："
				+ countryElement.getTextTrim());

		// 电子邮件electronicMailAddress
		Element electronicMailAddressElement = (Element) metadataElement
				.selectSingleNode(CswTool.electronicMailAddressXmlPath);
		if(metadataIso.getOrganizor()==null || metadataIso.getOrganizor().getElectronicMailAddress()==null)
			electronicMailAddressElement.setText("null");
		else
		    electronicMailAddressElement.setText(metadataIso.getOrganizor().getElectronicMailAddress());
		logger.info("电子邮件name：" + electronicMailAddressElement.getName()
				+ ",value：" + electronicMailAddressElement.getTextTrim());
		
		// 职责(资源维护者originator) 默认
		
		return metadataElement;

	}

	/**
	 * 获取资源元数据值
	 * @param metadataElement  元数据根元素gmd:MD_Metadata
	 * @return 返回设置好值的资源实体类
	 * @throws BusinessException
	 */
	public static MetadataIso getMetadata(Element metadataElement) throws BusinessException
	{	
		MetadataIso metadataIso=null;
		User organizor=null;
		
		metadataIso=new MetadataIso();
		organizor=new User();

		// 2.1设置标识信息
		// 语言
		Element languageElement = (Element) metadataElement
				.selectSingleNode(CswTool.languageXmlPath);
		if(languageElement!=null)
		{
			metadataIso.setLanguage(languageElement.getTextTrim());
			logger.info("语言name：" + languageElement.getName() + ",value："+ languageElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：语言language");

		// 资源名称title
		Element titleElement = (Element) metadataElement
				.selectSingleNode(CswTool.titleXmlPath);
		if(titleElement!=null)
		{
			metadataIso.setTitle(titleElement.getTextTrim());
			logger.info("资源名称name：" + titleElement.getName() + ",value："+ titleElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：资源名称title");

		// 资源描述 abstract(metadataAbstract)
		Element metadataAbstractElement = (Element) metadataElement
				.selectSingleNode(CswTool.metadataAbstractXmlPath);
		if(metadataAbstractElement!=null)
		{
			metadataIso.setMetadataAbstract(metadataAbstractElement.getTextTrim());
			logger.info("资源描述name：" + metadataAbstractElement.getName()+ ",value：" + metadataAbstractElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：资源描述 abstract");

		// 关键字 keyword(metadataKeyword)
		Element metadataKeywordElement = (Element) metadataElement
				.selectSingleNode(CswTool.metadataKeywordXmlPath);
		if(metadataKeywordElement!=null)
		{
			metadataIso.setMetadataKeyword(metadataKeywordElement.getTextTrim());
			logger.info("关键字 name：" + metadataKeywordElement.getName()
					+ ",value：" + metadataKeywordElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：关键字 keyword");

		// 时间范围-开始时间 beginPosition 2012-10-10T15:39:00
		Element beginPositionElement = (Element) metadataElement
				.selectSingleNode(CswTool.beginPositionXmlPath);
		if(beginPositionElement!=null)
		{
			String temp=beginPositionElement.getTextTrim();
			if(!temp.equals("null"))
			{
				metadataIso.setBeginPosition(DateTimeTool.getJavaDate(temp));	
				logger.info("时间范围-开始时间name：" + beginPositionElement.getName()+ ",value：" + temp);
			} //end if
		} //end if
		else
			logger.warn("GetRecords返回的结果中没有的值：时间范围-开始时间 beginPosition");

		// 时间范围-结束时间 endPosition 2012-10-10T15:39:00
		Element endPositionElement = (Element) metadataElement
				.selectSingleNode(CswTool.endPositionXmlPath);
		if(endPositionElement!=null)
		{
			String temp=endPositionElement.getTextTrim();
			if(!temp.equals("null"))
			{
				metadataIso.setEndPosition(DateTimeTool.getJavaDate(temp));
				logger.info("时间范围-结束时间 name：" + endPositionElement.getName()+ ",value：" + temp);
			} //end if
		} //end if
		else
			logger.warn("GetRecords返回的结果中没有的值：时间范围-结束时间 endPosition");
		
		// 空间范围-北纬 参考 northBoundLatitude GN图上选择空间区域
		Element northBoundLatitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.northBoundLatitudeXmlPath);
		if(northBoundLatitudeElement!=null)
		{
			metadataIso.setNorthBoundLatitude(Double.valueOf(northBoundLatitudeElement.getTextTrim()));
			logger.info("空间范围-北纬name：" + northBoundLatitudeElement.getName()+ ",value：" + northBoundLatitudeElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：空间范围-北纬 参考 northBoundLatitude");

		// 空间范围-南纬 southBoundLatitude
		Element southBoundLatitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.southBoundLatitudeXmlPath);
		if(southBoundLatitudeElement!=null)
		{
			metadataIso.setSouthBoundLatitude(Double.valueOf(southBoundLatitudeElement.getTextTrim()));
			logger.info("空间范围-南纬name：" + southBoundLatitudeElement.getName()+ ",value：" + southBoundLatitudeElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：空间范围-南纬 southBoundLatitude");

		// 空间范围-西经 westBoundLongitude
		Element westBoundLongitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.westBoundLongitudeXmlPath);
		if(westBoundLongitudeElement!=null)
		{
			metadataIso.setWestBoundLongitude(Double.valueOf(westBoundLongitudeElement.getTextTrim()));
			logger.info("空间范围-西经name：" + westBoundLongitudeElement.getName()+ ",value：" + westBoundLongitudeElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：空间范围-西经 westBoundLongitude");

		// 空间范围-东经 eastBoundLongitude
		Element eastBoundLongitudeElement = (Element) metadataElement
				.selectSingleNode(CswTool.eastBoundLongitudeXmlPath);
		if(eastBoundLongitudeElement!=null)
		{
			metadataIso.setEastBoundLongitude(Double.valueOf(eastBoundLongitudeElement.getTextTrim()));
			logger.info("空间范围-东经name：" + eastBoundLongitudeElement.getName()+ ",value：" + eastBoundLongitudeElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：空间范围-东经 eastBoundLongitude");

		// 分类/节点类型/服务类别 topicCategory
		Element topicCategoryElement = (Element) metadataElement
				.selectSingleNode(CswTool.topicCategoryXmlPath);
		if(topicCategoryElement!=null)
		{
			metadataIso.setTopicCategory(topicCategoryElement.getTextTrim());
			logger.info("节点类型name：" + topicCategoryElement.getName()+ ",value：" + topicCategoryElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：分类/节点类型/服务类别 topicCategory");

		// 发布时间date(publicationDate)
		Element publicationDateElement = (Element) metadataElement
				.selectSingleNode(CswTool.publicationDateXmlPath);
		if(publicationDateElement!=null)
		{
			String temp=publicationDateElement.getTextTrim();
			if(!temp.equals("null"))
			{
				metadataIso.setPublicationDate(DateTimeTool.getJavaDate(temp));
				logger.info("发布时间name：" + publicationDateElement.getName()+ ",value：" + temp);
			} //end if
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：发布时间date");
		
		//------------------------------------------------------
		//获取资源限制信息(数据类型、数据量大小、数据条目个数)
		List<Element> otherConstraintsElList=metadataElement.selectNodes(CswTool.otherConstraintsXmlPath);
		if(otherConstraintsElList!=null && otherConstraintsElList.size()>0 )
		{
			//数据类型
			metadataIso.setDataType(otherConstraintsElList.get(0).getTextTrim());
			logger.info("数据类型：" + otherConstraintsElList.get(0).getTextTrim());
			//数据量大小
			metadataIso.setDataSize(otherConstraintsElList.get(1).getTextTrim());
			logger.info("数据量大小：" + otherConstraintsElList.get(1).getTextTrim());
			//数据条目个数
			metadataIso.setDataNumber(otherConstraintsElList.get(2).getTextTrim());
			logger.info("数据条目个数：" + otherConstraintsElList.get(2).getTextTrim());
			//传感器
			metadataIso.setSensor(otherConstraintsElList.get(3).getTextTrim());
			logger.info("传感器：" + otherConstraintsElList.get(3).getTextTrim());
		} //end if
		else
			logger.warn("GetRecords返回的结果中没有的值：数据类型、数据量大小、数据条目个数、传感器");
		
		//补充信息
		Element supplementElement=(Element)metadataElement.selectSingleNode(CswTool.supplementXmlPath);
		if(supplementElement!=null)
		{
			metadataIso.setSupplement(supplementElement.getTextTrim());
			logger.info("补充信息name：" + supplementElement.getName() + ",value："+ supplementElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：补充信supplement");
		//------------------------------------------------------
		
		// 2.2发布信息
		// 访问地址/服务访问点linkage
//		Element linkageElement = (Element) metadataElement
//				.selectSingleNode(CswTool.linkageXmlPath);
//		if(linkageElement!=null)
//		{
//			metadataIso.setLinkage(linkageElement.getTextTrim());
//			logger.info("访问地址name：" + linkageElement.getName() + ",value："+ linkageElement.getTextTrim());
//		}
//		else
//			logger.warn("GetRecords返回的结果中没有的值：访问地址/服务访问点linkage");
//
//		// 服务类型/服务模型 protocol（枚举值：参照GN的列表值和UDDI的列表值）
//		Element protocolElement = (Element) metadataElement
//				.selectSingleNode(CswTool.protocolXmlPath);
//		if(protocolElement!=null)
//		{
//			metadataIso.setProtocol(protocolElement.getTextTrim());
//			logger.info("服务类型name：" + protocolElement.getName() + ",value："+ protocolElement.getTextTrim());
//		}
//		else
//			logger.warn("GetRecords返回的结果中没有的值：服务类型 protocol");
		
		List<Element> linkageElList =metadataElement.selectNodes(CswTool.linkageXmlPath);
		if(linkageElList!=null && linkageElList.size()>0)
		{
			String urlXmlPath="./gmd:CI_OnlineResource/gmd:linkage/gmd:URL";               	//服务地址
			String proXmlPath="./gmd:CI_OnlineResource/gmd:protocol/gco:CharacterString";   //服务类型
			
			Element linkageElement=(Element)linkageElList.get(0).selectSingleNode(urlXmlPath);
			Element protocolElement=(Element)linkageElList.get(0).selectSingleNode(proXmlPath);
			//访问地址/服务访问点linkage
			if(linkageElement!=null)
			{
				metadataIso.setLinkage(linkageElement.getTextTrim());
				logger.info("服务地址name：" + linkageElement.getName() + ",value："+ linkageElement.getTextTrim());
			}	
			else
				logger.warn("GetRecords返回的结果中没有的值：服务访问地址linkage");
			// 服务类型/服务模型 protocol
			if(protocolElement!=null)
			{
				metadataIso.setProtocol(protocolElement.getTextTrim());
				logger.info("服务类型name：" + protocolElement.getName() + ",value："+ protocolElement.getTextTrim());
			}
			else
				logger.warn("GetRecords返回的结果中没有的值：服务类型protocol");
							
			//portal访问地址
			Element portalLinkageElement=(Element)linkageElList.get(1).selectSingleNode(urlXmlPath);
			if(portalLinkageElement!=null)
			{
				metadataIso.setPortalLinkage(portalLinkageElement.getTextTrim());
				logger.info("potal服务地址name：" + portalLinkageElement.getName() + ",value："+ portalLinkageElement.getTextTrim());
			}
			else
				logger.warn("GetRecords返回的结果中没有的值：potal服务地址portalLinkage");
			
			//--add-----
			Set<String> linkageInterfaceSet=new HashSet<String>();
			for(int i=2;i<linkageElList.size();i++){
				Element lingkageInterfaceElement=(Element)linkageElList.get(i).selectSingleNode(urlXmlPath);
				if(lingkageInterfaceElement!=null)
				{
					linkageInterfaceSet.add(lingkageInterfaceElement.getTextTrim());
					logger.info("服务端访问接口name：" + lingkageInterfaceElement.getName() + ",value："+ lingkageInterfaceElement.getTextTrim());
				}
				else
					logger.warn("GetRecords返回的结果中没有的值：服务端访问接口linkageInterface");
			} //end for
			
			metadataIso.setLinkageInterface(linkageInterfaceSet);
			//--end add----
			
		} //end if
		
		
		

		// 2.3数据质量信息
		// 质量描述
		Element qualityElement = (Element) metadataElement
				.selectSingleNode(CswTool.qualityXmlPath);
		if(qualityElement!=null)
		{
			metadataIso.setQuality(qualityElement.getTextTrim());
			logger.info("质量信息name：" + qualityElement.getName() + ",value："+ qualityElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：质量描述quality");

		// 2.4元数据信息
		// 标识符UDDI fileIdentifier 由GN自动生成
		Element fileIdentifierElement = (Element) metadataElement
				.selectSingleNode(CswTool.fileIdentifierXmlPath);
		if(fileIdentifierElement!=null)
		{
			metadataIso.setFileIdentifier(fileIdentifierElement.getTextTrim());
			logger.info("标识符UDDIname：" + fileIdentifierElement.getName()+ "，value：" + fileIdentifierElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：标识符UDDI fileIdentifier");

		// 字符集 Character（utf8） characterSet 默认utf8，界面显示UTF8

		// 修改时间dateStamp(modifyDate) 格式（YYYY-MM-DDThh:mm:ss）2012-10-11T17:27:20
		Element modifyDateElement = (Element) metadataElement
				.selectSingleNode(CswTool.modifyDateXmlPath);
		if(modifyDateElement!=null)
		{
			String temp=modifyDateElement.getTextTrim();
			if(!temp.equals("null"))
			{
				metadataIso.setModifyDate(DateTimeTool.getJavaDate(temp));
				logger.info("修改时间name：" + modifyDateElement.getName()+ ",value：" + modifyDateElement.getTextTrim());
			} //end if
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：修改时间modifyDate");

		// 元数据标准名 默认

		// 元数据标准版本 Metadata standard version 默认1.0

		// 2.5 资源组织
		// 组织名称organisationName
		Element organisationNameElement = (Element) metadataElement
				.selectSingleNode(CswTool.organisationNameXmlPath);
		if(organisationNameElement!=null)
		{
			organizor.setOrganisationName(organisationNameElement.getTextTrim());
			logger.info("组织名称name：" + organisationNameElement.getName()+ ",value：" + organisationNameElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：组织名称organisationName");

		// 负责人姓名individualName
		Element individualNameElement = (Element) metadataElement
				.selectSingleNode(CswTool.individualNameXmlPath);
		//individualNameElement.setText(metadataIso.getOrganizor().getIndividualName());
		if(individualNameElement!=null)
		{
			organizor.setIndividualName(individualNameElement.getTextTrim());
			logger.info("负责人姓名name：" + individualNameElement.getName()+ ",value：" + individualNameElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：负责人姓名individualName");
		
		// 联系电话 voice
		Element voiceElement = (Element) metadataElement
				.selectSingleNode(CswTool.voiceXmlPath);
		//voiceElement.setText(metadataIso.getOrganizor().getVoice());
		if(voiceElement!=null)
		{
			organizor.setVoice(voiceElement.getTextTrim());
			logger.info("联系电话name：" + voiceElement.getName() + ",value："+ voiceElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：联系电话 voice");


		// 详细地址 deliveryPoint
		Element deliveryPointElement = (Element) metadataElement
				.selectSingleNode(CswTool.deliveryPointXmlPath);
		//deliveryPointElement.setText(metadataIso.getOrganizor().getDeliveryPoint());
		if(deliveryPointElement!=null)
		{
			organizor.setDeliveryPoint(deliveryPointElement.getTextTrim());
			logger.info("详细地址name：" + deliveryPointElement.getName()+ ",value：" + deliveryPointElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：详细地址 deliveryPoint");
			
		// 城市（兰州市） city
		Element cityElement = (Element) metadataElement
				.selectSingleNode(CswTool.cityXmlPath);
		if(cityElement!=null)
		{
			organizor.setCity(cityElement.getTextTrim());
			logger.info("城市name：" + cityElement.getName() + ",value："+ cityElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：城市city");

		// 行政区域(甘肃省)administrativeArea
		Element administrativeAreaElement = (Element) metadataElement
				.selectSingleNode(CswTool.administrativeAreaXmlPath);
		if(administrativeAreaElement!=null)
		{
			organizor.setAdministrativeArea(administrativeAreaElement.getTextTrim());
			logger.info("行政区域name：" + administrativeAreaElement.getName()+ ",value：" + administrativeAreaElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：行政区域administrativeArea");

		// 邮政编码postalCode
		Element postalCodeElement = (Element) metadataElement
				.selectSingleNode(CswTool.postalCodeXmlPath);
		if(postalCodeElement!=null)
		{
			organizor.setPostalCode(postalCodeElement.getTextTrim());
			logger.info("邮政编码name：" + postalCodeElement.getName()+ ",value：" + postalCodeElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：邮政编码postalCode");

		// 国家/组织所在地 country
		Element countryElement = (Element) metadataElement
				.selectSingleNode(CswTool.countryXmlPath);
		if(countryElement!=null)
		{
			organizor.setCountry(countryElement.getTextTrim());
			logger.info("国家name：" + countryElement.getName() + ",国家value："+ countryElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：国家/组织所在地 country");

		// 电子邮件electronicMailAddress
		Element electronicMailAddressElement = (Element) metadataElement
				.selectSingleNode(CswTool.electronicMailAddressXmlPath);
		if(electronicMailAddressElement!=null)
		{
			organizor.setElectronicMailAddress(electronicMailAddressElement.getTextTrim());
			logger.info("电子邮件name：" + electronicMailAddressElement.getName()+ ",value：" + electronicMailAddressElement.getTextTrim());
		}
		else
			logger.warn("GetRecords返回的结果中没有的值：电子邮件electronicMailAddress");
		
		// 职责(资源维护者originator) 默认
		
		//设置资源组织信息
		metadataIso.setOrganizor(organizor);
		
		return metadataIso;

	}

	
	public static void main(String[] args) {
		// CswTool.login();
		//insertXml();

	}

}
